using System;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        // 十六进制转换为double
        private void btnToDouble_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexValue = txtHex.Text.Trim().Replace(" ", "");
                if (string.IsNullOrEmpty(hexValue))
                {
                    MessageBox.Show("请输入有效的十六进制值");
                    return;
                }
                
                byte[] bytes = HexStringToByteArray(hexValue);
                if (bytes.Length < 8)
                {
                    Array.Resize(ref bytes, 8);
                }
                
                double result = BitConverter.ToDouble(bytes, 0);
                txtNumber.Text = result.ToString("0.0###########");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // 十六进制转换为float
        private void btnToFloat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexValue = txtHex.Text.Trim().Replace(" ", "");
                if (string.IsNullOrEmpty(hexValue))
                {
                    MessageBox.Show("请输入有效的十六进制值");
                    return;
                }
                
                byte[] bytes = HexStringToByteArray(hexValue);
                if (bytes.Length < 4)
                {
                    Array.Resize(ref bytes, 4);
                }
                
                float result = BitConverter.ToSingle(bytes, 0);
                txtNumber.Text = result.ToString("0.0") + "f";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // double转换为十六进制
        private void btnFromDouble_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!double.TryParse(txtNumber.Text, out double value))
                {
                    MessageBox.Show("请输入有效的double值");
                    return;
                }
                
                byte[] bytes = BitConverter.GetBytes(value);
                txtHex.Text = ByteArrayToHexString(bytes);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // float转换为十六进制
        private void btnFromFloat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!float.TryParse(txtNumber.Text, out float value))
                {
                    MessageBox.Show("请输入有效的float值");
                    return;
                }
                
                byte[] bytes = BitConverter.GetBytes(value);
                txtHex.Text = ByteArrayToHexString(bytes);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // 十六进制转换为int32
        private void btnToInt32_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexValue = txtHex.Text.Trim().Replace(" ", "");
                if (string.IsNullOrEmpty(hexValue))
                {
                    MessageBox.Show("请输入有效的十六进制值");
                    return;
                }
                
                byte[] bytes = HexStringToByteArray(hexValue);
                if (bytes.Length < 4)
                {
                    Array.Resize(ref bytes, 4);
                }
                
                int result = BitConverter.ToInt32(bytes, 0);
                txtNumber.Text = result.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // 十六进制转换为int64
        private void btnToInt64_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string hexValue = txtHex.Text.Trim().Replace(" ", "");
                if (string.IsNullOrEmpty(hexValue))
                {
                    MessageBox.Show("请输入有效的十六进制值");
                    return;
                }
                
                byte[] bytes = HexStringToByteArray(hexValue);
                if (bytes.Length < 8)
                {
                    Array.Resize(ref bytes, 8);
                }
                
                long result = BitConverter.ToInt64(bytes, 0);
                txtNumber.Text = result.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // int32转换为十六进制
        private void btnFromInt32_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!int.TryParse(txtNumber.Text, out int value))
                {
                    MessageBox.Show("请输入有效的int32值");
                    return;
                }
                
                byte[] bytes = BitConverter.GetBytes(value);
                txtHex.Text = ByteArrayToHexString(bytes);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // int64转换为十六进制
        private void btnFromInt64_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!long.TryParse(txtNumber.Text, out long value))
                {
                    MessageBox.Show("请输入有效的int64值");
                    return;
                }
                
                byte[] bytes = BitConverter.GetBytes(value);
                txtHex.Text = ByteArrayToHexString(bytes);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"转换错误: {ex.Message}");
            }
        }
        
        // 辅助方法：十六进制字符串转字节数组
        private byte[] HexStringToByteArray(string hex)
        {
            if (hex.Length % 2 != 0)
            {
                hex = "0" + hex; // 确保偶数长度
            }
            
            byte[] bytes = new byte[hex.Length / 2];
            for (int i = 0; i < hex.Length; i += 2)
            {
                bytes[i / 2] = byte.Parse(hex.Substring(i, 2), NumberStyles.HexNumber);
            }
            return bytes;
        }
        
        // 辅助方法：字节数组转十六进制字符串
        private string ByteArrayToHexString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                sb.Append(b.ToString("X2"));
            }
            return sb.ToString();
        }
    }
}